#ifndef MAINWINDOW_H
#define MAINWINDOW_H
/*
Here is the definition of the main window.
Including the menu and menu items.
*/
#include <QMainWindow>

class QAction;
class QLabel;
class QMenu;
class QWidget;
class QStackedLayout;
class QMdiArea;
class QSignalMapper;
class QTreeWidget;
class QTreeWidgetItem;
class QDockWidget;
class CSpreadsheet;
class QMdiSubWindow;
class GLWidget;
class CData;
class CFindDialog;


class CMainWindow : public QMainWindow
{
    Q_OBJECT
public:
    CMainWindow();
    ~CMainWindow();

protected:
    virtual void keyPressEvent( QKeyEvent *e );
    virtual void closeEvent(QCloseEvent* event); //is automatically called when the user closes the window
    virtual void timerEvent(QTimerEvent * e); // used for updating the status of the subwindows
                                              // for each time when a cell in a sub-window is selected
                                              // the slot in spreadsheet updateStatusBar() will set the main window
                                              // modified, then the timerEvent() handler will check the modification
                                              // and update the status bar accordingly.

private slots:
    void about();
    bool save();
    bool saveAs();
    bool saveAll();
    void open();
    void openProjectFile();
    bool showGraphics(bool bShow_); // bShow_ = true <> Graphics
    void showFullScreen(bool bFullScreen_); // bFullScreen_ = ture <> fullscreen mode
    void setFullScreen(bool bFullScreen_);
    void setDisplayMode(bool bShow_);
    void updateActions();
    void updateWindowMenu();
    void cut();
    void copy();
    void paste();
    void del();
    void cutCurrentRow();
    void insertRowAboveCurrent();
    void selectCurrentRow();
    void selectCurrentColumn();
    void selectAll();
    bool closeAll();
    bool closeSubWindow();
    void setActiveSubWindow(QWidget *pWindow_);
    void setActiveSubWindow(QTreeWidgetItem* pTree_);
    void openRecentProject();
    void updateTreeDock(QMdiSubWindow* pSubWindow_);
    //void setStatusBar(QMdiSubWindow* pSubWindow_);
    void spreadsheetModified();
    void runExperiment();
    void calibration();
    void exportData();
    void nextTrial();
    void setMaximizeSubWindow(QTreeWidgetItem* pTree_);
    void find();
    void findNext(const QString &sStr_, Qt::CaseSensitivity eCs_);
    void findPrevious(const QString &sStr_, Qt::CaseSensitivity eCs_);
    void exitAllTrial();
    void sort();
    void switchProject(QTreeWidgetItem* pTree_);


signals:
    void fullScreenChanged(bool bFullScreen_);  // 
    void displayModeChanged(bool bShow_); // 

private:
    void createActions();
    void createMenus();
    void createToolBars();


    bool loadProject(const QString &sFileName_);
    void setCurrentProject(const QString &sFileName_);
    void updateRecentProjectActions();
    void readSettings();
    void writeSettings();
    CSpreadsheet* activeSpreadsheet();
    void addSpreadsheet(CSpreadsheet* pSpreadsheet_);
    bool addNewProjectToTreeDock(QStringList& lPathFileNameExts_);
    void createFiles(QStringList& lPathFileNameExts_);
    void updateSubWindows(QStringList& lPathFileNameExts_);
    //bool updateProjectList(QStringList& lPathFileNameExts_);
    bool isProjectOpened(const QString& sPathProjectNameExt_);

    bool fileExistinTree(const QString& sFileName_);
    void openFiles(const QStringList& lFileNames_);
    void openFile(const QString& sFileName_);
   
    void createStatusBar();
    void updateStatusBar();
    void loadData();
    QStringList getFileNames(QString& sPathProjectExt_);
    void addNewFileToCurrentProject(const QString& sPathFileNameExt_);
    enum { MaxRecentFiles = 5 };


    CFindDialog* _pFindDialog;

    QStackedLayout* _pCentralLayout;

    QMdiArea*      _pMDIArea;
    QSignalMapper* _pWindowMapper;  //mapping the action lists in windows menu with sub-windows

    // Dock
    QDockWidget* _pDockWidget;
    QTreeWidget* _pTreeWidget;

    // Menus
    QMenu*     _pFileMenu;
    QMenu*     _pEditMenu;
    QMenu*     _pSelectSubMenu;
    QMenu*     _pViewMenu;
    QMenu*     _pOtherMenu;
    QMenu*     _pWindowMenu;
    QMenu*     _pHelpMenu;

    // Toolbar
    QToolBar*  _pFileToolBar;
    QToolBar*  _pEditToolBar;
    QToolBar*  _pSystemBar;
    QToolBar*  _pOtherToolBar;

    // File
    //QAction*   _pNewAct;
    QAction*   _pOpenAct;
    QAction*   _pAddFilesAct;
    QAction*   _pSaveAct;
    QAction*   _pSaveAsAct;
    QAction*   _pSaveAllAct;
    QAction*   _pExportAct;
    QAction*   _pExitAct;
    QAction*   _aRecentFileActions[MaxRecentFiles];
    QAction*   _pSeparatorAct1;

    // Edit
    QAction*   _pCutAct;
    QAction*   _pCopyAct;
    QAction*   _pPasteAct;
    QAction*   _pDeleteAct;
    QAction*   _pInsertAct;
    QAction*   _pSelectAllAct;//Select All
    QAction*   _pSelectRowAct;
    QAction*   _pSelectColumnAct;
    QAction*   _pFindAct;
    QAction*   _pGotoCellAct;
    QAction*   _pSortAct;

    // View
    QAction*   _pShowGfxAct;
    QAction*   _pFullScreenAct;
    QAction*   _pRunAct;
    QAction*   _pNextTrialAct;
    QAction*   _pStopExperimentAct;
    QAction*   _pCalibration;

    // Windows
    QAction*   _pCloseAct;
    QAction*   _pCloseAllAct;
    QAction*   _pTileAct;
    QAction*   _pCascadeAct;
    QAction*   _pNextAct;
    QAction*   _pPreviousAct;
    QAction*   _pSeparatorAct2;

    // About
    QAction*   _pAboutAct;
    QAction*   _pAboutQtAct;

    // Label for status bar
    QLabel*    _pLocationLabel;
    QLabel*    _pFormulaLabel;

    bool       _bFullscreen;
    bool       _bShowGraphics;

    GLWidget* _pGlWidget;          //graphics rendering widget
    //CGLCalibration* _pGLCalibration;

    CData*     _pData;

    /*static*/ QStringList _slRecentProjects;
    QString    _sCurPathProjectExt;
    /*  Format:
        QStringListList 1  < ".prj" QString, ".init" QString, ".spc" QString... >
        QStringListList 2  < ".prj" QString, ".init" QString, ".spc" QString... >
        ...
        QStringListList n  < ".prj" QString, ".init" QString, ".spc" QString... >    */
    //QList<QStringList> _llPathFileExts; // store all the 
    int        _nTimerID;
};

 #endif
